package com.yh.homestay.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.CapitalItemMapper;
import com.yh.homestay.pojo.CapitalItem;
import com.yh.homestay.service.CapitalItemService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName CapitalItemServiceImpl
 * @Description 商品业务逻辑
 * @Author GCL
 * @Date 2020/1/2 9:07
 */
@Service
public class CapitalItemServiceImpl implements CapitalItemService {

    private Log logger = LogFactory.getLog(CapitalItemServiceImpl.class);

    @Autowired
    private CapitalItemMapper capitalItemMapper;

    /**
     * @param page
     * @param limit
     * @param itemName 商品名称
     * @Date: 2020/1/11 11:17
     * @Description: 查询出所有商品信息
     * @return: com.yh.homestay.common.HomeStayResult
     */
    @Override
    public HomeStayResult findAllCapitalItems(Integer page, Integer limit, String itemName) {
        try {
            PageHelper.startPage(page, limit);
            List<CapitalItem> capitalItems;
            if (StringUtils.isEmpty(itemName)) {
                capitalItems = capitalItemMapper.selectAll();
            } else {
                Example example = new Example(CapitalItem.class);
                example.createCriteria().andLike("itemName", "%" + itemName + "%");
                capitalItems = capitalItemMapper.selectByExample(example);
            }
            PageInfo info = new PageInfo(capitalItems);
            return new HomeStayResult(0, "ok", (int) info.getTotal(), info.getList());
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param capitalItem
     * @Date: 2020/1/11 11:20
     * @Description: 添加商品逻辑
     * @return: com.yh.homestay.common.HomeStayResult
     */
    @Override
    public HomeStayResult addCapitalItem(CapitalItem capitalItem) {
        try {
            capitalItemMapper.insertSelective(capitalItem);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param id
     * @Date: 2020/1/11 11:21
     * @Description: 查询一个房间数据
     * @return: com.yh.homestay.common.ItemEditResult
     */
    @Override
    public HomeStayResult selectOneCapitalItem(Integer id) {
        try {
            CapitalItem capitalItem = capitalItemMapper.selectByPrimaryKey(id);
            return HomeStayResult.ok(200, capitalItem);
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param capitalItem
     * @Date: 2020/1/11 11:28
     * @Description: 更新商品信息
     * @return: com.yh.homestay.common.CapitalItemResult
     */
    @Override
    public HomeStayResult updateCapitalItem(CapitalItem capitalItem) {
        try {
            capitalItemMapper.updateByPrimaryKeySelective(capitalItem);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param itemId
     * @Date: 2020/1/11 11:30
     * @Description: 删除商品信息
     * @return: com.yh.homestay.common.CapitalItemResult
     */
    @Override
    public HomeStayResult delCapitalItem(Integer itemId) {
        try {
            capitalItemMapper.deleteByPrimaryKey(itemId);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param itemIds
     * @Date: 2020/1/11 11:33
     * @Description: 批量删除商品信息
     * @return: com.yh.homestay.common.CapitalItemResult
     */
    @Override
    public HomeStayResult delBatchCapitalItem(String itemIds) {
        try {
            String[] ids = itemIds.split(",");
            for (int i = 0; i < ids.length; i++) {
                capitalItemMapper.deleteByPrimaryKey(ids[i]);
            }
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }
}
